package cn.innosmart.aq.view.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.tutk.p2p.ConnectionManager;

import java.io.InputStream;
import java.util.Timer;
import java.util.TimerTask;

import cn.innosmart.aq.R;
import cn.innosmart.aq.adapter.FullscreenPTDeviceGridViewAdapter;
import cn.innosmart.aq.view.activity.base.BaseActivity;

public class FullscreenMonitoringActivity extends BaseActivity {
    private Toolbar toolbar;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 724:
                    deviceGridViewAdapter.refreshData();
                    break;
                case 725:
                    setOnlineOffline();
                    break;
                case EXIT:
                    exit();
                    break;
            }
        }
    };
    private Menu menus;
    private GridView gvDevice;
    private FullscreenPTDeviceGridViewAdapter deviceGridViewAdapter;
    private Timer timer;
    private IntentFilter onlineStatusListener;
    private ConnectionActionReceiver connectionActionReceiver;
    private TimerTask offlineSetTask;

    public Bitmap readBitMap(int resId) {

        BitmapFactory.Options opt = new BitmapFactory.Options();

        opt.inPreferredConfig = Bitmap.Config.ARGB_4444;

        opt.inPurgeable = true;

        opt.inInputShareable = true;

        // 获取资源图片

        InputStream is = this.getResources().openRawResource(resId);

        return BitmapFactory.decodeStream(is, null, opt);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fullscreen);

        gvDevice = (GridView) findViewById(R.id.gv_device);
        deviceGridViewAdapter = new FullscreenPTDeviceGridViewAdapter(this);
        gvDevice.setAdapter(deviceGridViewAdapter);

        Typeface tf_fzcy = Typeface.createFromAsset(getAssets(), "fonts/fzcy.ttf");
        TextView titleView = (TextView) findViewById(R.id.fullscreen_monitor_title);
        titleView.setTypeface(tf_fzcy);
        titleView.setText("智能水族监控系统");

//        Bitmap b = readBitMap(R.drawable.fullscreen_background);
//        BitmapDrawable ob = new BitmapDrawable(getResources(), b);
//        gvDevice.setBackgroundDrawable(ob);


        timer = new Timer();

        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                mHandler.sendEmptyMessage(724);
            }
        }, 0, 5000);


        initBroadcastListener();

        TextView onlineOfflineTV = (TextView) findViewById(R.id.fullscreen_monitor_online_offline);
        onlineOfflineTV.setText(R.string.gw_online);
        onlineOfflineTV.setTextColor(Color.GREEN);

        if (offlineSetTask != null) {
            offlineSetTask.cancel();
        }
        offlineSetTask = new TimerTask() {
            @Override
            public void run() {
                mHandler.sendEmptyMessage(725); //offline
            }
        };
        Timer timer2 = new Timer();
        timer2.schedule(offlineSetTask, 3 * 60 * 1000);

    }

    public void initBroadcastListener() {
        onlineStatusListener = new IntentFilter();
        onlineStatusListener.addAction("connect_event_online");
        onlineStatusListener.addAction("connect_event_offline");
        connectionActionReceiver = new ConnectionActionReceiver();
        registerReceiver(connectionActionReceiver, onlineStatusListener);
    }

    public void setOnlineOffline() {
        TextView onlineOfflineTV = (TextView) findViewById(R.id.fullscreen_monitor_online_offline);
        if(onlineOfflineTV.getCurrentTextColor() != Color.YELLOW) {
            onlineOfflineTV.setTextColor(Color.RED);
        }
        onlineOfflineTV.setText(R.string.camera_status_offline);
    }

    public class ConnectionActionReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {

            if (intent.getAction().equals("connect_event_online")) {
                TextView onlineOfflineTV = (TextView) findViewById(R.id.fullscreen_monitor_online_offline);
                onlineOfflineTV.setText(R.string.gw_online);
                onlineOfflineTV.setTextColor(Color.GREEN);
                mHandler.sendEmptyMessage(724);
            } else {
                TextView onlineOfflineTV = (TextView) findViewById(R.id.fullscreen_monitor_online_offline);
                onlineOfflineTV.setTextColor(Color.YELLOW);
                onlineOfflineTV.setText(R.string.camera_status_offline);
            }

            if (offlineSetTask != null) {
                offlineSetTask.cancel();
            }
            offlineSetTask = new TimerTask() {
                @Override
                public void run() {
                    mHandler.sendEmptyMessage(725); //offline
                }
            };
            Timer timer = new Timer();
            timer.schedule(offlineSetTask, 3 * 60 * 1000);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();

        timer.cancel();
    }

    private int backCount = 0;
    long lastBackPressed = 0;

    @Override
    public void onBackPressed() {
        long now = System.currentTimeMillis();

        if (now - lastBackPressed > 5000) {
            lastBackPressed = now;
            return;
        }

        if (now - lastBackPressed <= 5000) {
            backCount++;
            if (backCount >= 10) {
                lastBackPressed = now;
                super.onBackPressed();
            }
        }
    }
}
